package net.xiduth.game.player.content.citadels;

import net.xiduth.Settings;
import net.xiduth.cores.CoresManager;
import net.xiduth.game.RegionBuilder;
import net.xiduth.game.WorldTile;
import net.xiduth.game.player.Player;
import net.xiduth.game.player.content.FadingScreen;
import net.xiduth.game.tasks.WorldTask;
import net.xiduth.game.tasks.WorldTasksManager;
import net.xiduth.utils.Logger;

/**
 * Represents a 'Player Owned Citadel'
 * 
 * @author Jae
 * 
 */
public class Citadel {

	public static transient int[] mapBaseCoords;

	/**
	 * Instance of a citadel, used to build it.
	 * 
	 * @param player
	 */
	public Citadel(final Player player) {
		// new Tier(0);//setting up objects.
		BuildCitadel(player);
	}

	public static WorldTile ClanCitadel;
	public static int NORTH = 0, SOUTH = 1, EAST = 2, WEST = 3;

	/**
	 * builds the citadel and places the player on the map
	 */
	public static void BuildCitadel(final Player player) {
		player.lock();
		final long time = FadingScreen.fade(player);
		CoresManager.slowExecutor.execute(new Runnable() {
			@Override
			public void run() {
				try {
					player.closeInterfaces();
					player.lock();
					CoresManager.slowExecutor.execute(new Runnable() {
						@Override
						public void run() {
							try {
								boolean needDestroy = mapBaseCoords != null;
								final int[] oldMapBaseCoords = mapBaseCoords;
								mapBaseCoords = RegionBuilder
										.findEmptyChunkBound(40, 40);
								RegionBuilder.copyAllPlanesMap(568, 576,
										mapBaseCoords[0], mapBaseCoords[1], 40);
								if (needDestroy) {
									WorldTasksManager.schedule(new WorldTask() {
										@Override
										public void run() {
											CoresManager.slowExecutor
													.execute(new Runnable() {
														@Override
														public void run() {
															try {
																RegionBuilder
																		.destroyMap(
																				oldMapBaseCoords[0],
																				oldMapBaseCoords[1],
																				8,
																				8);
															} catch (Exception e) {
																e.printStackTrace();
															} catch (Error e) {
																e.printStackTrace();
															}
														}
													});
										}
									});
								}
							} catch (Exception e) {
								e.printStackTrace();
							} catch (Error e) {
								e.printStackTrace();
							}

						}
					});
					FadingScreen.unfade(player, time, new Runnable() {
						@Override
						public void run() {
							AddPlayerToCitadel(player);
							player.unlock();
						}
					});

				} catch (Throwable e) {
					Logger.handle(e);
				}
			}
		});

	}

	/**
	 * Safely removes the citadel off the map
	 * 
	 * @param logout
	 * @param player
	 */

	public static void DestroyCitadel(final boolean logout, final Player player) {
		RegionBuilder.destroyMap(mapBaseCoords[0], mapBaseCoords[1], 40, 40);
	}

	/**
	 * represents the owner
	 */
	public Object getOwnerOfCitadel(final Player player) {
		return player.getTemporaryAttributtes().get("owner");

	}

	/**
	 * removes the owner(TODO)
	 * 
	 * @param whichOne
	 */
	public void RemoveOwner(Player whichOne) {
		whichOne.getTemporaryAttributtes().get("owner".equals(null));

	}

	/**
	 * When the owner is added to their citadel. ONLY THE OWNER.
	 * 
	 * @param player
	 */
	private static void AddPlayerToCitadel(final Player player) {
		player.getTemporaryAttributtes().put("owner", player.getDisplayName());
		player.getControlerManager().startControler("ClanCitadel");
		player.setNextFaceWorldTile(new WorldTile(player.getX() + 0, player
				.getY() - 1, 0));
		player.lock();
		player.setNextWorldTile(new WorldTile(getBaseX() + 242,
				getBaseY() + 40, 0));
		player.getMusicsManager().playMusic(485);
	}

	/**
	 * Allows one to leave
	 * 
	 * @param player
	 */
	public static void LeaveCitadel(final Player player) {
		player.setNextWorldTile(Settings.RESPAWN_PLAYER_LOCATION);
		player.getControlerManager().removeControlerWithoutCheck();

	}

	/**
	 * returns baseX of the citadel
	 * 
	 * @return mapBaseCoords[0] << 3;
	 */
	public static int getBaseX() {
		return mapBaseCoords[0] << 3;
	}

	/**
	 * returns the baseY of the citadel
	 * 
	 * @return mapBaseCoords[1] << 3;
	 */
	public static int getBaseY() {
		return mapBaseCoords[1] << 3;
	}

	/**
	 * Allows an owner to kick a player from their citadel
	 * 
	 * @param player
	 */
	public void Kick(final Player player) {
		if (player.getDisplayName() != player.getTemporaryAttributtes().get(
				"owner")) {
			player.getPackets().sendGameMessage(
					"You are not the owner of this citadel.");
			return;
		}

		// TODO kicking

	}
}
